package com.infoland.afanmall.model.fuPin.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.StatusBarKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.layoutManager.GridSpacingItemDecoration;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.fuPin.adapter.FpCunActivityAdapter;
import com.infoland.afanmall.model.fuPin.entity.FuPinCunResponse;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wang.avi.AVLoadingIndicatorView;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.vondear.rxtools.RxTool.getContext;

/**
 * @author 王永吉
 * @date 2018/7/3 11:24
 * 查看村子列表
 */
public class FpCunListActivity extends AppCompatActivity {
    @BindView(R.id.fp_city_recycle)
    RecyclerView fpCityRecycle;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.sd)
    ImageView sd;
    @BindView(R.id.fp_include_cun_title)
    LinearLayout fpIncludeCunTitle;
    @BindView(R.id.avi_pt)
    AVLoadingIndicatorView aviPt;
    FpCunActivityAdapter fpCunActivityAdapter;
    @BindView(R.id.header)
    MaterialHeader header;
    @BindView(R.id.fp_cun_ref)
    SmartRefreshLayout fpCunRef;
    int fupinguanId = 0;

    private int getLayout() {
        return R.layout.fu_pin_cun;
    }

    private void initData() {
        back.setOnClickListener(v -> {
            finish();
        });
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            refLoadStart();
            initPage();
            fupinguanId = extras.getInt("fupinguanId");
            getCunZi(fupinguanId);
        }
        /*** 刷新**/
        fpCunRef.setOnRefreshListener(refreshLayout -> {
            initPage();
            getCunZi(fupinguanId);
        });

        /*** 加载更多**/
        fpCunRef.setOnLoadMoreListener(refreshLayout -> {
            refLoadStart();
            getCunZi(fupinguanId);
        });

    }


    /*** 查询村子**/
    private void getCunZi(int fupinguanId) {
        if (getPage()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pageNumber", pageNumber);
            jsonObject.put("pageSize", CONST.FU_PIN_CUN_PAGE_SIZE);
            jsonObject.put("fupinguanId", fupinguanId);

            new OkHttpKit().send(
                    response200 -> {
                        LogKit.E("查询村子 ---->   ", "  " + response200);
                        FuPinCunResponse fuPinCunResponse = JSONObject.parseObject(response200, FuPinCunResponse.class);
                        FuPinCunResponse.ObjectBean object = fuPinCunResponse.getObject();
                        setFuPinCunZi(fuPinCunResponse);
                        refLoadOk();
                    },
                    error401 -> {
                        LogKit.E("success401 ---->   ", "  " + error401);
                    },
                    error -> {
                        refLoadError();
                        LogKit.E("error ---->   ", "  " + error);
                    }
            ).post(CONST.FU_PIN_CUN, jsonObject, this);
        }

    }

    /*** 设置扶贫村子**/
    private void setFuPinCunZi(FuPinCunResponse fuPinCunResponse) {
        if (fuPinCunResponse.isSuccess()) {
            setPage(fuPinCunResponse.getObject().getPageNumber(), fuPinCunResponse.getObject().isLastPage());
            fpCunActivityAdapter = new FpCunActivityAdapter(R.layout.fu_pin_cun_activity_item, fuPinCunResponse.getObject().getList());
            fpCunActivityAdapter.setOnItemClickListener((adapter, view, position) -> {
                FuPinCunResponse.ObjectBean.ListBean o =
                        (FuPinCunResponse.ObjectBean.ListBean) adapter.getData().get(position);
                Bundle bundle = new Bundle();
                bundle.putInt("fupincunId", o.getId());
                ToolsKit.go(FpCunInfoActivity.class, bundle);

            });
            fpCityRecycle.setAdapter(fpCunActivityAdapter);
        } else {
        }
    }


    int pageNumber = 1;
    boolean lastPAge = false;

    private void initPage() {
        this.pageNumber = 1;
        this.lastPAge = false;
    }

    private void setPage(int pageNumber, boolean lastPAge) {
        this.pageNumber = (pageNumber + 1);
        this.lastPAge = lastPAge;
    }

    private boolean getPage() {
        if (lastPAge) {
            refLoadError();
            ToastUtils.showShort("已经是最后一页");
            return false;
        } else {
            return true;
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        StatusBarKit.immersiveBlackTitleText(this, fpIncludeCunTitle);//沉浸黑字
        StatusBarKit.setMargin(this, fpCunRef);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
        fpCityRecycle.setLayoutManager(gridLayoutManager);
        fpCityRecycle.addItemDecoration(new GridSpacingItemDecoration(
                3, SizeUtils.dp2px(12),
                true));

        initData();
    }


    /*** 加载或刷新失败**/
    private void refLoadError() {
        fpCunRef.setEnableRefresh(true);
        fpCunRef.setEnableLoadMore(true);
        fpCunRef.finishRefresh(500);
        fpCunRef.finishLoadMore();
        aviPt.setVisibility(View.GONE);
    }

    /*** 加载或刷新完成**/
    private void refLoadOk() {
        fpCunRef.setEnableRefresh(true);
        fpCunRef.setEnableLoadMore(true);
        fpCunRef.finishRefresh(500);
        fpCunRef.finishLoadMore();
        aviPt.setVisibility(View.GONE);
    }

    /*** 加载或刷新开始**/
    private void refLoadStart() {
        fpCunRef.setEnableRefresh(false);
        fpCunRef.setEnableLoadMore(false);
        aviPt.setVisibility(View.VISIBLE);
    }


}
